package com.zeedev.flyingsaucer.data;

import android.net.Uri;
import android.net.Uri.Builder;
import android.text.TextUtils;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Beers;

import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;

public class BeerUri implements Serializable {
	
	public Boolean isTasted = null;
	public Boolean isActive = null;
	
	public String groupBy = null;
	public String sortBy = null;
	
	public String memberId = null;
	public String memberDbId = null;
	public String storeDbId = null;
	
	public Boolean addMemberBeer = null;
	public Boolean addStoreBeer = null;
	
	Map<String, Set<String>> propertySelectionValuesMap = null;
	
	public BeerUri() {
		super();
		propertySelectionValuesMap = new HashMap<String, Set<String>>();
	}
	
	public Uri getUri(Uri headUri) {
		Builder b = headUri.buildUpon();
		
		if (!TextUtils.isEmpty(sortBy)) {
			b.appendQueryParameter(FlyingSaucerConv.SORT_BY, sortBy);
		}
		
		if (!TextUtils.isEmpty(groupBy)) {
			b.appendQueryParameter(FlyingSaucerConv.GROUP_BY, groupBy);
		}
		
		if (!TextUtils.isEmpty(memberId)) {
			b.appendQueryParameter(FlyingSaucerConv.BY_MEMBER_ID, memberId);
		}
		
		if (!TextUtils.isEmpty(memberDbId)) {
			b.appendQueryParameter(FlyingSaucerConv.BY_MEMBER_DB_ID, memberDbId);
		}
		
		if (!TextUtils.isEmpty(storeDbId)) {
			b.appendQueryParameter(FlyingSaucerConv.BY_STORE_DB_ID, storeDbId);
		}
		
		if (addMemberBeer != null) {
			if (addMemberBeer) {
				b.appendQueryParameter(FlyingSaucerConv.ADD_STANDARD_MEMBER_BEER_COLUMNS, "true");
			}
		}
		
		if (addStoreBeer != null) {
			if (addStoreBeer) {
				b.appendQueryParameter(FlyingSaucerConv.ADD_STANDARD_STORE_BEER_COLUMNS, "true");
			}
		}
		
		if (isTasted != null) {
			if (isTasted) {
				b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "true");
			} else {
				b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "false");
			}
		}
		
		if (isActive != null) {
			if (isActive) {
				b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
			} else {
				b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "false");
			}
		}
		
		for (Entry<String, Set<String>> entry : this.getPropertySelectionValuesMap().entrySet()) {
			String curGroup = entry.getKey();
			String curParam = null;
			if (curGroup.equals(Beers.CONTAINER)) {
				curParam = FlyingSaucerConv.FILTER_CONTAINER;
			}
			else if (curGroup.equals(Beers.BREWER_NAME)) {
				curParam = FlyingSaucerConv.FILTER_BREWERS;
			}
			else if (curGroup.equals(Beers.STYLE_NAME)) {
				curParam = FlyingSaucerConv.FILTER_STYLES;
			}
			else if (curGroup.equals(Beers.COUNTRY_NAME)) {
				curParam = FlyingSaucerConv.FILTER_COUNTRIES;
			}
			Set<String> curSelection = entry.getValue();
//			String selectionWithCommas = withCommas(curSelection);
			StringBuilder sb = new StringBuilder();
//			SQLiteQueryBuilder.appendColumns(sb, curSelection.toArray(new String[]{}));
			Iterator<String> iter = curSelection.iterator();
			
			 while (iter.hasNext()) {
				
				String curString = iter.next();
				sb.append( " '" + curString + "' " );
				if (iter.hasNext()) {
					sb.append(" , ");
				}
				
			}
			
			
			
			if (!TextUtils.isEmpty(sb.toString())) {
				b.appendQueryParameter(curParam, sb.toString());
			}
			
		}

		return b.build();	
		
	}
	
	
	
	
	






	
	
	public void setPropertySelectionValuesMap(
			Map<String, Set<String>> propertySelectionValuesMap) {
		this.propertySelectionValuesMap = propertySelectionValuesMap;
	}
	
	public Map<String, Set<String>> getPropertySelectionValuesMap() {
		return propertySelectionValuesMap;
	}
	
	public Set<String> getGroupedSelectionSet(String groupBy) {
		Set<String> tset = this.propertySelectionValuesMap.get(groupBy);
		if (tset == null){
			propertySelectionValuesMap.put(groupBy, new HashSet<String>());	
		}
		return this.propertySelectionValuesMap.get(groupBy);
	}
	
	public Boolean hasSelection() {
		Boolean ret = false;
		for (String curKey: this.propertySelectionValuesMap.keySet() ) {
			ret = ret || !this.propertySelectionValuesMap.get(curKey).isEmpty();
		}
		ret = ret || !this.propertySelectionValuesMap.isEmpty();
		return ret;
	}
	
	public Boolean hasOneEmptySelection() {
		Boolean ret = false;
		for (Entry <String, Set<String>>  entry: this.propertySelectionValuesMap.entrySet() ) {
			if (entry.getValue().isEmpty()) {
				return true;
			}
		}
		return ret;
	}
	
	

}
